﻿using System;

using NHibernate;
using DflyingForum.Core.Data.NHHelper;

namespace DflyingForum.Core.Repositories
{
    /// <summary>
    /// 事务CRUD类
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    /// <typeparam name="TKey"></typeparam>
    public class NHTransactionHelper<TEntity, TKey> where TEntity : class
    {
        private ISession _session = SessionManager.GetCurrentSession();

        /// <summary>
        /// 事务插入数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public TKey InsertTransaction(TEntity entity)
        {
            TKey key;
            using (ITransaction trans = _session.BeginTransaction())
            {
                try
                {
                    key = (TKey)_session.Save(entity);
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    throw new Exception("数据插入失败\r\n" + ex.Message, ex);
                }
            }
            return key;
        }

        /// <summary>
        /// 事务插入数据
        /// </summary>
        /// <param name="entity"></param>
        public void UpdateTransaction(TEntity entity)
        {
            using (ITransaction trans = _session.BeginTransaction())
            {
                try
                {
                    _session.Update(entity);
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    throw new Exception("数据更新失败\r\n" + ex.Message, ex);
                }
            }
        }

        /// <summary>
        /// 事务删除数据
        /// </summary>
        /// <param name="entity"></param>
        public void DeleteTransaction(TEntity entity)
        {
            using (ITransaction trans = _session.BeginTransaction())
            {
                try
                {
                    _session.Delete(entity);
                    trans.Commit();
                }
                catch (Exception ex)
                {
                    trans.Rollback();
                    throw new Exception("数据删除失败\r\n" + ex.Message, ex);
                }
            }
        }

        public void Transaction(Action action)
        {
            using (ITransaction transaction = _session.BeginTransaction())
            {
                try
                {
                    action();
                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }

        public void Transaction(Action action, System.Data.IsolationLevel isolationLevel)
        {
            using (ITransaction transaction = _session.BeginTransaction(isolationLevel))
            {
                try
                {
                    action();
                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();
                    throw;
                }
            }
        }

    }
}